{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Syntax Error or Indentation Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T03:53:10.099479Z",
     "start_time": "2018-08-27T03:53:10.093494Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "Missing parentheses in call to 'print'. Did you mean print('Hello')? (<ipython-input-1-ec3fee0488db>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-1-ec3fee0488db>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    print 'Hello'\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print('Hello')?\n"
     ]
    }
   ],
   "source": [
    "print 'Hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-3-81d2d0504a04>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-81d2d0504a04>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    if a = b:\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 2\n",
    "\n",
    "if a = b:\n",
    "    print('Equal')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-4-32081bd4c085>, line 6)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-4-32081bd4c085>\"\u001b[1;36m, line \u001b[1;32m6\u001b[0m\n\u001b[1;33m    else:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "if 2 == 2:\n",
    "    print('Equal')\n",
    "\n",
    "print('Out of if block')\n",
    "\n",
    "else:\n",
    "    print('Inside else')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T03:53:22.430707Z",
     "start_time": "2018-08-27T03:53:22.419736Z"
    }
   },
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unexpected indent (<ipython-input-2-e77ebde98481>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-e77ebde98481>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    print 'World'\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n"
     ]
    }
   ],
   "source": [
    "print('Hello')\n",
    " print 'World'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Runtime Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T03:58:31.865052Z",
     "start_time": "2018-08-27T03:58:31.494336Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# print('Hello')\n",
    "# prnt('World')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:10:09.231912Z",
     "start_time": "2018-08-27T04:10:09.220941Z"
    }
   },
   "outputs": [],
   "source": [
    "# print(1)\n",
    "\n",
    "# print(1/0)\n",
    "\n",
    "# print('Hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:15:28.469968Z",
     "start_time": "2018-08-27T04:15:21.115600Z"
    }
   },
   "outputs": [],
   "source": [
    "# print('*'*50)\n",
    "\n",
    "# x = int(input('Enter an Integer Number: '))\n",
    "\n",
    "# print(x)\n",
    "\n",
    "# print('a'+ x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:13:16.903927Z",
     "start_time": "2018-08-27T04:13:16.893955Z"
    }
   },
   "outputs": [],
   "source": [
    "# print('Start of the program')\n",
    "\n",
    "# x = int('one')\n",
    "\n",
    "# print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## try........except:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:27:05.685014Z",
     "start_time": "2018-08-27T04:27:05.674046Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    },
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-15-8d907ebc3424>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Hello'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "# without try-except\n",
    "\n",
    "print(1)\n",
    "\n",
    "print(1/0)\n",
    "\n",
    "print('Hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:27:40.279074Z",
     "start_time": "2018-08-27T04:27:40.274087Z"
    }
   },
   "outputs": [],
   "source": [
    "# print(1)\n",
    "\n",
    "# try:\n",
    "#     print(1/0)\n",
    "# except:\n",
    "#     print(10/5)\n",
    "\n",
    "# print('Hello')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Control Flow in try.......except"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:35:23.589598Z",
     "start_time": "2018-08-27T04:35:23.581620Z"
    }
   },
   "outputs": [],
   "source": [
    "# '''\n",
    "# Cases:\n",
    "# 1. No exception\n",
    "\n",
    "# 2. exception in stmt 1 to 3\n",
    "\n",
    "# 3. exception at stmt 5\n",
    "\n",
    "# 4. exception in stmt 1 to 3 and corresponding except block\n",
    "\n",
    "# '''\n",
    "\n",
    "# print('stmt - 0')\n",
    "# try:\n",
    "#     print('stmt - 1')\n",
    "#     print('stmt - 2')\n",
    "#     print('stmt - 3')\n",
    "# except:\n",
    "#     print('stmt - 4')\n",
    "    \n",
    "# print('stmt - 5')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## try With Multiple except"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:50:24.464916Z",
     "start_time": "2018-08-27T04:50:15.414883Z"
    }
   },
   "outputs": [],
   "source": [
    "# '''\n",
    "# If try with multiple except blocks available then default except block \n",
    "# should be last,otherwise we will get SyntaxError.\n",
    "# '''\n",
    "\n",
    "# try:\n",
    "#     x = int(input('Enter First Number: '))\n",
    "#     y = int(input('Enter Second Number: '))\n",
    "#     print(x/y)\n",
    "    \n",
    "# except ValueError:\n",
    "#     print(\"Please enter integer value only\")\n",
    "    \n",
    "# except ZeroDivisionError:\n",
    "#     print(\"You can't divide with Zero\")\n",
    "\n",
    "# except:\n",
    "#     print('I dont know, but somethings wrong!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:44:05.308233Z",
     "start_time": "2018-08-27T04:43:58.924076Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter First Number: 1\n",
      "Enter Second Number: 0\n",
      "Somethings wrong!\n"
     ]
    }
   ],
   "source": [
    "# Single except can handle multiple exceptions\n",
    "\n",
    "try:\n",
    "    x = int(input('Enter First Number: '))\n",
    "    y = int(input('Enter Second Number: '))\n",
    "    print(x/y)\n",
    "except (ZeroDivisionError, ValueError):\n",
    "    print('Somethings wrong!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Printing Exception as Message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T04:39:47.219846Z",
     "start_time": "2018-08-27T04:39:47.211871Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exception: division by zero\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(10/0)\n",
    "except ZeroDivisionError as message:\n",
    "    print('Exception:', message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## finally block"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:10:38.662182Z",
     "start_time": "2018-08-27T05:10:38.656560Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "try block\n",
      "finally block\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print('try block')\n",
    "except:\n",
    "    print('except block')\n",
    "finally:\n",
    "    print('finally block')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:11:48.691314Z",
     "start_time": "2018-08-27T05:11:48.685818Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "try block\n",
      "except block\n",
      "finally block\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print('try block')\n",
    "    print(1/0)\n",
    "except ZeroDivisionError:\n",
    "    print('except block')\n",
    "finally:\n",
    "    print('finally block')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Tricky Question\n",
    "\n",
    "# try:\n",
    "#     print('try block')\n",
    "#     print(1/0)\n",
    "# except:\n",
    "#     prit('except block')\n",
    "# finally:\n",
    "#     print('finally block')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # Tricky Question\n",
    "\n",
    "# try:\n",
    "#     print('try block')\n",
    "#     prit(1/2)\n",
    "# except ZeroDivisionError:\n",
    "#     print('except block')\n",
    "# finally:\n",
    "#     print('finally block')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:12:51.985372Z",
     "start_time": "2018-08-27T05:12:51.978803Z"
    }
   },
   "outputs": [],
   "source": [
    "# Is ordering Important?\n",
    "\n",
    "# try:\n",
    "#     print('try block')\n",
    "# finally:\n",
    "#     print('finally block')\n",
    "# except:\n",
    "#     print('except block')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## try....except.....else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:13:53.191176Z",
     "start_time": "2018-08-27T05:13:53.184135Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "try\n",
      "else\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(\"try\")\n",
    "except:\n",
    "    print(\"except\")\n",
    "else:\n",
    "    print(\"else\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:14:38.768993Z",
     "start_time": "2018-08-27T05:14:38.762796Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "try\n",
      "except\n",
      "finally\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    print(\"try\")\n",
    "    print(1/0)\n",
    "except:\n",
    "    print(\"except\")\n",
    "else:\n",
    "    print(\"else\")\n",
    "finally:\n",
    "    print(\"finally\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:14:50.352061Z",
     "start_time": "2018-08-27T05:14:50.344853Z"
    }
   },
   "outputs": [],
   "source": [
    "# Ordering?\n",
    "\n",
    "# try:\n",
    "#     print(\"try\")\n",
    "#     print(10/0)\n",
    "# except:\n",
    "#     print(\"except\")\n",
    "# finally:\n",
    "#     print(\"finally\")\n",
    "# else:\n",
    "#     print(\"else\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nested try....except....finally"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:19:59.921327Z",
     "start_time": "2018-08-27T05:19:59.906057Z"
    }
   },
   "outputs": [],
   "source": [
    "# # Tricky Question\n",
    "\n",
    "# try:\n",
    "#     print('Outer try block')\n",
    "#     try:\n",
    "#         print('Inner try block')\n",
    "#         print(10/0)\n",
    "#     except ValueError:\n",
    "#         print('Inner except block')\n",
    "#     finally:\n",
    "#         print('Inner finally block')\n",
    "# except ValueError:\n",
    "#     print('Outer except block')\n",
    "# finally:\n",
    "#     print('Outer finally block')\n",
    "    \n",
    "# # try ValueError on both"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## raise an exception"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:26:41.076794Z",
     "start_time": "2018-08-27T05:26:39.546803Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your age1\n",
      "you are too young....so i am gonna through an exception\n",
      "get out\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    age = int(input('Enter your age'))\n",
    "    if(age<10):\n",
    "        print('you are too young....so i am gonna through an exception')\n",
    "        raise ZeroDivisionError('get out')\n",
    "except ZeroDivisionError as message:\n",
    "    print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exception Hierarchy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](exception.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## User Defined Exception"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-26T17:35:01.817882Z",
     "start_time": "2018-08-26T17:35:01.813895Z"
    }
   },
   "outputs": [],
   "source": [
    "# Also known as customized exception\n",
    "\n",
    "# Programmer is responsible to define and raise these exceptions\n",
    "\n",
    "# class classname(predefined exception class):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-27T05:35:52.641493Z",
     "start_time": "2018-08-27T05:35:44.421969Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter your Age: 10\n",
      "You can Enter!\n"
     ]
    }
   ],
   "source": [
    "class TooYoungException(Exception):\n",
    "    def __init__(self, msg):\n",
    "        self.msg = msg\n",
    "\n",
    "try:\n",
    "    age = int(input('Enter your Age: '))\n",
    "    if(age<10):\n",
    "        raise TooYoungException('You are too young!')\n",
    "    else:\n",
    "        print('You can Enter!')\n",
    "except TooYoungException as msg:\n",
    "    print(msg)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
